import Vue from 'vue';
import Vuex from 'vuex';
import {websocketUrl} from "../api/api";

Vue.use(Vuex);
// 登录验证
export default new Vuex.Store({
    state: {
      companyInfo:[],
      ws:{},
        user: false,
        addReturn:{
          resList:{
            newList:[],
            oldList:[]
          },
          resMap:{
            newList:[],
            oldList:[]
          }
        },
      addReturn1:{
        resList:{
          newList:[],
          oldList:[]
        },
        resMap:{
          newList:[],
          oldList:[]
        }
      },
      selectValue:null

    },
    mutations: {
      setCompanyInfo(state, data) {
        state.companyInfo=data
      },
      setSelectValue(state, value) {
        state.selectValue = value;
      },
      initWebSocket(state) {
        let ws
        if ('WebSocket' in window) {
          ws = new WebSocket(websocketUrl + this.uuid);
        } else if ('MozWebSocket' in window) {
          ws = new MozWebSocket(websocketUrl + this.uuid);
        }else {
          this.$Notice.warning({
            title: '浏览器过低',
            desc: '请升级浏览器'
          })
        }
        ws.onmessage = function (evt) {
          if (evt.data.state === 200) {
            this.modalTable = true;
          }

        };

        ws.onclose = function (evt) {
          console.log("连接中断");
        };

        ws.onopen = function (evt) {
          console.log("连接成功");
        };
        state.ws=ws;

      },
        // 登录
        login(state, user) {
            state.user = user;
            localStorage.setItem("userInfo", user);
        },
        // 退出
        logout(state, user) {
            state.user = "";
            localStorage.setItem("userInfo", "");
        },
      setAddReturn(state, data) {
        if (data.resMap) {
          state.addReturn.resMap=data.resMap;

        }
        if (data.resList) {
          state.addReturn.resList=data.resList
        }

      },
      setAddReturn1(state, data) {
        if (data.resMap) {
          state.addReturn1.resMap=data.resMap;

        }
        if (data.resList) {
          state.addReturn1.resList=data.resList
        }
      }
    },
  actions:{
    login(context, user) {
      context.commit('login', user);
    }
  }
})
